package org.chatboleh;

import java.awt.Color;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

import org.apache.log4j.Logger;
import org.chatboleh.bean.TextElement;
import org.chatboleh.bean.User;
import org.chatboleh.exception.EDMException;
import org.chatboleh.tools.UserFactory;
import org.chatboleh.util.InchToPoint;
import org.chatboleh.util.MD5;
import org.chatboleh.util.PrintableDocument;
import org.hsqldb.Server;

public class Tester {

	private static Logger logger = Logger.getLogger(Tester.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// String value = "11111111111.11";
		// logger.debug(value);
		// logger.debug(CurrencyToWords.convert(value, Language.ENGLISH));
		// logger.debug(CurrencyToWords.convert(20.50, Language.ENGLISH));
		Tester self = new Tester();
		// self.CurrentDirectoryTest();
//		 self.JDBCTest();
//		self.HSQLDBServerTest();
		 self.getUserListTest();
		//self.Printest();
		// User user = UserFacroty.getInstance().createUser("ongternchow",
		// "inigomantoya", "ternchow.ong@gmail.com");
		// logger.debug(user);
		// User user = new User(0);
		// logger.debug(user);
		// user.setPassword("alpha02");
		// try {
		// user.save();
		// } catch (EDMException e) {
		// logger.error(e);
		// }
		// logger.debug(user);

		// TextElement te = new TextElement(1);
		// logger.debug(te);
		// logger.debug(""+Color.decode("-14336"));
		// logger.debug("12 inch = " + InchToPoint.convert(12));
		// logger.debug("305 mm = " + InchToPoint.convertMM(305));

	}

	private void HSQLDBServerTest(){
//		Server server = new Server();
		try {
			Server.main(new String[]{"-database","C:/Users/caslyn/Desktop/TernChow/Eclipse/Workspace/Enterprice Document/test02"});
		} catch (Exception e) {
			logger.error(e);
		}
	}
	
	private void getUserListTest() {
		List<User> list = UserFactory.getInstance().getUserList("id>2");
		for (User user : list) {
			logger.debug(user);
		}
		// User user = new User(3);
		// try {
		// user = UserFacroty.getInstance().createUser("NANA",
		// MD5.getInstance().hashData(new Date().toString().getBytes()),
		// "test@gmail.com");
		// } catch (NoSuchAlgorithmException e) {
		// e.printStackTrace();
		// }
		// logger.debug(user);
	}

	private void CurrentDirectoryTest() {
		File dir1 = new File(".");
		File dir2 = new File("..");
		try {
			System.out.println("Current dir : " + dir1.getCanonicalPath());
			System.out.println("Parent  dir : " + dir2.getCanonicalPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void JDBCTest() {
		String url = "jdbc:hsqldb:hsql://localhost/edmdb";
		String sql = "create cached table user(id identity, name varchar(20),password varchar(20))";
		// sql = "select * from user";
		sql = "insert into user( name, password) values('xxx','"
				+ new Date().toString() + "')";
		Connection conn = null;
		try {
			Class.forName("org.hsqldb.jdbcDriver");
//			conn = DriverManager
//			.getConnection(
//					"jdbc:hsqldb:C:/Users/caslyn/Desktop/TernChow/Eclipse/Workspace/Enterprice Document/test01",
//					"sa", "");
			conn = DriverManager.getConnection(url, "sa", "");
			sql = "select * from user";
//			PreparedStatement ps = conn.prepareStatement(sql);
//			ps.executeUpdate();
//			sql = "select * from user";
//			ResultSet rs = ps.executeQuery(sql);
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				logger.debug(rs.getLong(1) + "\t" + rs.getString(2) + "\t"
						+ rs.getString(3));
			}
		} catch (SQLException e) {
			logger.error(e);
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			logger.error(e);
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error(e);
			}
		}

	}

	private void Printest() {

		PrinterJob printJob = PrinterJob.getPrinterJob();
		// DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
		// PrintRequestAttributeSet attr_set = new
		// HashPrintRequestAttributeSet();
		// attr_set.add(MediaSizeName.ISO_A4);
		// attr_set.add(new JobName("My Print Job", Locale.ENGLISH));
		// attr_set.add(new MediaPrintableArea(0, 0, 300, 500,
		// MediaPrintableArea.MM));
		// PrintService[] pservices = PrintServiceLookup.lookupPrintServices(
		// flavor, attr_set);
		// if (pservices.length > 0) {
		// // DocPrintJob pj = pservices[0].createPrintJob();
		// DocPrintJob pj = pservices[0].createPrintJob();
		// try {
		// FileInputStream fis = new FileInputStream("test.ps");
		// Doc doc = new SimpleDoc(fis, flavor, null);
		// pj.print(doc, attr_set);
		// } catch (FileNotFoundException fe) {
		// fe.printStackTrace();
		// } catch (PrintException e) {
		// e.printStackTrace();
		// }
		// }

		// PageFormat pf = printJob.pageDialog(printJob.defaultPage());

		PrintableDocument doc = new PrintableDocument();

		TextElement te = new TextElement();
		te.setText("Hellow World.");
		te.setFontSize(16);
		te.setFontColor(Color.black);
		te.setX(50);
		te.setY(50);
		doc.addTextElement(te);
		te = new TextElement();
		te.setText("How are you.");
		te.setFontSize(8);
		te.setX(50);
		te.setY(100);
		doc.addTextElement(te);

		printJob.setPrintable(doc);
		printJob.setJobName(new Date().getSeconds() + "SS");
		// try {
		// printJob.setPrintService(pservices[0]);
		// } catch (PrinterException e) {
		// e.printStackTrace();
		// }
		AttributeSet attr_set = new HashAttributeSet();
		attr_set.add(new PrinterName("PrimoPDF", null));
		PrintService[] prinservice = PrintServiceLookup.lookupPrintServices(
				null, attr_set);

		if (prinservice.length > 0) {
			logger.debug("PrimoPDF found.");
			try {
				printJob.setPrintService(prinservice[0]);
			} catch (PrinterException e) {
				e.printStackTrace();
			}
			logger.debug("New = " + printJob.getPrintService().getName());
		}
		if (printJob.printDialog()) {
			try {
				printJob.print();
			} catch (PrinterException pe) {
				logger.debug("Error printing: " + pe);
			}
		}
		// logger.debug("Old = "+printJob.getPrintService().getName());
		// printJob.printDialog();

		// PrintService[] services3 =
		// PrintServiceLookup.lookupPrintServices(null, null);
		// for (PrintService printService : services3) {
		// logger.debug(printService.getName());
		// }

	}

}
